package com.aliyun.heiyu.powermaster.server.rotary.utils;

import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * @Author czx
 * @Description
 * @Version 2019-01-01 22:13
 */
public class HttpUtil {
    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    private static CloseableHttpClient getHttpClient() {
        return HttpClients.createDefault();
    }

    public static JSONObject postRequest(String json, String host) {
        JSONObject jsonobj = null;
        //创建http连接调用第三方接口
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(host);
            if (json != null) {
                StringEntity requestEntity = new StringEntity(json);
                requestEntity.setContentEncoding("UTF-8");
                requestEntity.setContentType("application/json");
                //使用post请求将json数据作为请求体
                httpPost.setEntity(requestEntity);
            }
            //发送请求，处理响应
            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                //判断响应码，看是否请求成功
                if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    return null;
                }
                jsonobj = new JSONObject(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            logger.error(e.getMessage(), e);
        } catch (ParseException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
        return jsonobj;
    }
    public static JSONObject postRequest(String json, Map<String, String> headMap, String host) {
        JSONObject jsonobj = null;
        //创建http连接调用第三方接口
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(host);
            if (json != null) {
                StringEntity requestEntity = new StringEntity(json);
                requestEntity.setContentEncoding("UTF-8");
                requestEntity.setContentType("application/json");
                //使用post请求将json数据作为请求体
                httpPost.addHeader("record_time",headMap.get("record_time"));
                httpPost.setEntity(requestEntity);
            }
            //发送请求，处理响应
            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                //判断响应码，看是否请求成功
                if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    return null;
                }
                jsonobj = new JSONObject(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            logger.error(e.getMessage(), e);
        } catch (ParseException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
        return jsonobj;
    }
}
